package com.caocaog.market.toccpt.jiguang;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import java.util.Map;

/**
 * 极光订单推送
 *
 * @author chechen
 * @date 2022-02-28
 */
public class JiGuangOrderPushUtil {

    private static final String APP_KEY = "8d000747eb0fd671033e61cc";
    private static final String MASTER_SECRET = "ff971f6bcf651e5927bc7c16";

    public static void pushOne(Map<String, String> extras, String alert, String alias) {

        // 创建JPushClient
        JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
        // 创建option
        PushPayload payload = PushPayload.newBuilder()
                // 所有平台的用户
                .setPlatform(Platform.all())
                // registrationId指定用户
                //.setAudience(Audience.registrationId(param.get("id")))
                .setAudience(Audience.alias(alias))
                .setNotification(Notification.newBuilder()
                        // 发送ios
                        .addPlatformNotification(IosNotification.newBuilder()
                                // 消息体
                                // .setAlert(param.get("msg"))
                                .setAlert(alert)
                                .setBadge(+1)
                                // ios提示音
                                .setSound("happy")
                                // 附加参数
                                .addExtras(extras)
                                .build())
                        // 发送android
                        .addPlatformNotification(AndroidNotification.newBuilder()
                                // 附加参数
                                .addExtras(extras)
                                // 消息体
                                .setAlert(alert)
                                .build())
                        .build())
                // 指定开发环境 true为生产模式 false 为测试模式 (android不区分模式,ios区分模式)
                .setOptions(Options.newBuilder().setApnsProduction(true).build())
                // 自定义信息
                .setMessage(Message.newBuilder().setMsgContent(alert).addExtras(extras).build())
                .build();
        try {
            PushResult pu = jpushClient.sendPush(payload);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

}
